package com.wnhz.work.yz.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.BathChooseBean;
import com.wnhz.work.yz.bean.BathroomConditionBean;
import com.wnhz.work.yz.bean.NormsBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * ---浴室情况
 */
public class BathroomConditionActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    private TextView tv_right;
    private String packageId;
    private String from;
    private BathroomConditionBean bean = new BathroomConditionBean();
    private List<BathChooseBean> skuattrList = new ArrayList<>();
    private TextView store;

    public static Intent createIntent(Context context, String packageId, String from) {
        Intent intent = new Intent(context, BathroomConditionActivity.class);
        intent.putExtra("packageId", packageId);
        intent.putExtra("from", from);
        return intent;
    }

    public void parseIntent(){
        Intent intent = getIntent();
        packageId = intent.getStringExtra("packageId");
        from = intent.getStringExtra("from");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bathroom_condition);
        parseIntent();
        initView();
    }

    private void initView() {
        store = (TextView) findViewById(R.id.store);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("浴室情况");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.submit).setVisibility(View.VISIBLE);
        findViewById(R.id.submit).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.submit);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("下一步");
        tv_right.setTextColor(0xff008c99);
        upload();
    }

    private static final String TAG = "BathroomCondition";

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        map.put("packageId", packageId);
        XUtil.Post(Url.PACKAGE_GETPACKAGESKULIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    if ("1".equals(res)) {
                        bean = new BathroomConditionBean();
                        Gson gson = new Gson();

                        bean = gson.fromJson(jsonObject.optString("info"), BathroomConditionBean.class);

                        JSONObject jsonObject1 = jsonObject.optJSONObject("info");
                        JSONObject jsonObject2 = jsonObject1.optJSONObject("sku_attr");

                        //   Log.e("-------", jsonObject1.optJSONArray("sku").length() + "");
                        Iterator iterator = jsonObject2.keys();                       // jsonObject2是JSONObject对象
                        while (iterator.hasNext()) {
                            String key = iterator.next() + "";
                            String v = jsonObject2.optString(key);
                            BathChooseBean skuBean = new BathChooseBean();
                            skuBean = gson.fromJson(v, BathChooseBean.class);
                            skuBean.setKey(key);
                            Log.e(TAG, "onSuccess:key=111 " + key);
                            skuattrList.add(skuBean);
                        }
                        bean.setSkuattr(skuattrList);

                        setData();

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                tv_right.setOnClickListener(BathroomConditionActivity.this);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
            }
        });
    }

    private RecyclerView recycler;

    private void setData() {
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        for (int i = 0; i < bean.getSku().size(); i++) {
            bean.getSku().get(i).getData().get(0).setStatus(1);
        }
        getStore();
        recycler.setAdapter(new BaseRVAdapter(this, bean.getSku()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_bathroom;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                holder.setTextView(R.id.text, bean.getSku().get(position).getSkuName());
                RecyclerView recycler = holder.getView(R.id.recycler);
                if ("0".equals(bean.getSku().get(position).getPosition())) {
                    recycler.setLayoutManager(new LinearLayoutManager(BathroomConditionActivity.this, LinearLayoutManager.HORIZONTAL, false));
                } else {
                    recycler.setLayoutManager(new LinearLayoutManager(BathroomConditionActivity.this, LinearLayoutManager.VERTICAL, false));
                }
                final List<NormsBean> data = bean.getSku().get(position).getData();
                recycler.setAdapter(new BaseRVAdapter(BathroomConditionActivity.this, data) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_norm;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, final int position1) {
                        holder.setTextView(R.id.text, data.get(position1).getName());
                        if (data.get(position1).getStatus() == 1) {
                            holder.setImageResource(R.id.select, R.mipmap.ic_choose_li);
                        } else {
                            holder.setImageResource(R.id.select, R.mipmap.ic_choose_nor);
                        }
                        holder.getView(R.id.select_ll).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                for (int i = 0; i < data.size(); i++) {
                                    if (i == position1) {
                                        data.get(i).setStatus(1);
                                    } else {
                                        data.get(i).setStatus(0);
                                    }
                                }
                                getStore();
                                notifyDataSetChanged();
                            }
                        });

                    }

                });

            }

        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            //下一步
            case R.id.submit:
                try {
                    List<Integer> a = new ArrayList<>();
                    if (bean.getSku() != null && bean.getSku().size() != 0) {

                        for (int i = 0; i < bean.getSku().size(); i++) {
                            for (int j = 0; j < bean.getSku().get(i).getData().size(); j++) {
                                if (bean.getSku().get(i).getData().get(j).getStatus() == 1) {
                                    a.add(Integer.parseInt((bean.getSku().get(i).getData().get(j).getSku_id())));
                                }
                            }
                        }

                        Collections.sort(a);
                        String str = "-";
                        for (int i = 0; i < a.size(); i++) {
                            str += a.get(i) + "-";
                        }
                        String skuId = "";
                        String store = "";
                        for (int i = 0; i < skuattrList.size(); i++) {
                            Log.e(TAG, "setData:==1= " + str + "      " + skuattrList.get(i).getKey());
                            if (str.equals(skuattrList.get(i).getKey())) {
                                skuId = skuattrList.get(i).getSku_id();
                                store = skuattrList.get(i).getStore();

                                if(from.equals("1")){
                                    if (null != bean) {
                                        Intent intent = new Intent(this, SingleAllocation2Activity.class);
                                        intent.putExtra("packageId", packageId);
                                        intent.putExtra("skuId", skuId);
                                        intent.putExtra("colorId", "");
                                        startActivity(intent);
                                    }
                                }else if(from.equals("2")){
                                    MyApplication.getInstance().setPricefale(1);
                                    Intent intent = new Intent(this, SingleAllocationActivity.class);
                                    intent.putExtra("packageId", packageId);
                                    intent.putExtra("skuId", skuId);
                                    intent.putExtra("colorId", "");
                                    intent.putExtra("goods_id", packageId);
                                    startActivity(intent);
                                }

                                if ("0".equals(store)) {
                                    MyToast("没有库存了！");
                                }
                                break;
                            }
                        }


                    } else {

                    }
                } catch (Exception e) {
                }
                break;
        }
    }

    public Object getStore() {
        List<Integer> a = new ArrayList<>();
        for (int i = 0; i < bean.getSku().size(); i++) {
            for (int j = 0; j < bean.getSku().get(i).getData().size(); j++) {
                if (bean.getSku().get(i).getData().get(j).getStatus() == 1) {
                    a.add(Integer.parseInt((bean.getSku().get(i).getData().get(j).getSku_id())));
                }
            }
        }

        Collections.sort(a);
        String str = "-";
        for (int i = 0; i < a.size(); i++) {
            str += a.get(i) + "-";
        }
        String skuId = "";
        String store = "";
        for (int i = 0; i < skuattrList.size(); i++) {
            Log.e(TAG, "setData:==1= " + str + "      " + skuattrList.get(i).getKey());
            if (str.equals(skuattrList.get(i).getKey())) {
                store = skuattrList.get(i).getStore();
                this.store.setText(store);
                break;
            }else{
                this.store.setText("0");
            }
        }

        return store;
    }
}
